package com.warm.pump.module.sys.service.impl;

import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.util.Constant;
import com.warm.pump.module.sys.bean.SysMenuDto;
import com.warm.pump.module.sys.bean.po.gen.SysMenu;
import com.warm.pump.module.sys.dao.gen.SysMenuDao;
import com.warm.pump.module.sys.service.ISysMenuService;
import com.warm.pump.module.sys.service.ISysRoleMenuService;
import com.warm.pump.module.sys.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理实现类
 *
 */
@Service("sysMenuService")
public class SysMenuServiceImpl implements ISysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    /**
     * 根据父菜单，查询子菜单
     *
     * @param parentId   父菜单ID
     * @param menuIdList 用户菜单ID
     */
    public List<SysMenu> queryListParentId(Long parentId, List<Long> menuIdList) {
        Map<String,Object> menuCondMap = new HashMap<>();
        menuCondMap.put("parentId",parentId);
        menuCondMap.put("orderBy"," order_num asc ");
        List<SysMenu> menuList = sysMenuDao.getListByMap(menuCondMap);
        if (menuIdList == null) {
            return menuList;
        }

        List<SysMenu> userMenuList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    /**
     * 根据父菜单，查询子菜单
     *
     * @param parentId 父菜单ID
     */
    public List<SysMenu> queryListParentId(Long parentId) {
        Map<String,Object> menuCondMap = new HashMap<>();
        menuCondMap.put("parentId",parentId);
        menuCondMap.put("orderBy"," order_num asc ");
        List<SysMenu> menuList = sysMenuDao.getListByMap(menuCondMap);
        return menuList;
    }

    /**
     * 获取不包含按钮的菜单列表
     */
    public List<SysMenu> queryNotButtonList() {
        Map<String,Object> menuCondMap = new HashMap<>();
        menuCondMap.put("type_not",2);
        menuCondMap.put("orderBy"," order_num asc ");
        return sysMenuDao.getListByMap(menuCondMap);
    }

    /**
     * 获取用户菜单列表
     */
    public List<SysMenuDto> getUserMenuList(Long userId) {
        // 系统管理员，拥有最高权限
        if (userId == 1) {
            return getAllMenuList(null);
        }

        // 用户菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    /**
     * 查询菜单
     */
    public SysMenu queryObject(Long menuId) {
        return sysMenuDao.selectByPrimaryKey(menuId);
    }

    /**
     * 查询菜单列表
     */
    public List<SysMenu> queryList(Map<String, Object> map) {
        return sysMenuDao.getListByMap(map);
    }

    /**
     * 查询总数
     */
    public int queryTotal(Map<String, Object> map) {
        return sysMenuDao.countTotalByMap(map);
    }

    /**
     * 保存菜单
     */
    public void save(SysMenu menu) {
        sysMenuDao.insertSelective(menu);
    }

    /**
     * 修改
     */
    public void update(SysMenu menu) {
        sysMenuDao.updateByPrimaryKeySelective(menu);
    }

    /**
     * 删除
     */
    @Transactional
    public void deleteBatch(Long[] menuIds) {
        Map<String,Object> condMap = new HashMap<>();
        condMap.put("menuId_in",menuIds);
        sysMenuDao.deleteByMap(condMap);
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenuDto> getAllMenuList(List<Long> menuIdList) {
        // 查询根菜单列表
        List<SysMenu> menuList = queryListParentId(0L, menuIdList);

        // 递归获取子菜单
        return  getMenuTreeList(menuList, menuIdList);
    }

    /**
     * 递归
     */
    private List<SysMenuDto> getMenuTreeList(List<SysMenu> menuList, List<Long> menuIdList) {
        List<SysMenuDto> subMenuList = new ArrayList<>();

        for (SysMenu entity : menuList) {
            SysMenuDto menuDto = new SysMenuDto();
            try {
                MyBeanUtils.copyBean2Bean(menuDto,entity);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (entity.getType() == Constant.MenuType.CATALOG.getValue()) {// 目录
                menuDto.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(menuDto);
        }

        return subMenuList;
    }
}
